//
//  TopTitleView.m
//  MuchEffort
//
//  Created by 陈雪丹 on 2017/5/4.
//  Copyright © 2017年 cxd. All rights reserved.
//

#import "TopTitleView.h"

@interface TopTitleView ()

@property (nonatomic, assign)NSInteger currentTag;

@end

@implementation TopTitleView

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    CGFloat btnW = kScreenWidth / _titleArray.count;
    CGFloat btnH = 30;
    CGFloat btnY = 4;
    for (NSInteger i = 0; i < _titleArray.count; i++) {
        UIButton *navBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        navBtn.frame = CGRectMake(btnW * i, btnY, btnW, btnH);
        [self addSubview:navBtn];
        [navBtn setTitle:_titleArray[i] forState:UIControlStateNormal];
        navBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [navBtn setTitleColor:MainDarkGrayColor forState:UIControlStateNormal];
        navBtn.backgroundColor = [UIColor clearColor];
        navBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        navBtn.tag = i + 1;
        [navBtn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.currentTag = 1;
    [(UIButton*)[self viewWithTag:1] setTitleColor:MainGreenColor forState:UIControlStateNormal];
    self.backgroundColor = [UIColor whiteColor];
    self.tipView.frame = CGRectMake(12, self.height - 3, btnW - 24, 3);
    _tipView.layer.cornerRadius = 1.5;
    [self addSubview:self.tipView];
}

- (void)btnDidClicked:(UIButton *)sender {
    if (_justTap) {
        self.tipView.centerX = sender.centerX;
    }
    [self changeBtnColor:sender.tag];
    if (self.clickBtnBlock) {
        self.clickBtnBlock(sender.tag);
    }
}

- (void)changeBtnColor:(NSInteger)tagNum {
    if (self.currentTag != 0) {
        [(UIButton *)[self viewWithTag:self.currentTag] setTitleColor:MainDarkGrayColor forState:UIControlStateNormal];
    }
    [(UIButton *)[self viewWithTag:tagNum] setTitleColor:MainGreenColor forState:UIControlStateNormal];
    self.currentTag = tagNum;
}

#pragma mark - 懒加载
- (UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectZero];
        _tipView.backgroundColor = MainGreenColor;
    }
    return _tipView;
}

@end
